#region Summary

// 
// Owner : JackieHan 
// 
// Solution : UpdateSystem 
// 
// Project : Skomart.Update.Config 
// 
// File : UpdateConfigItem.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 更新配置项
// 
// History : 
//			2010-08-02 11:50:34	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml;
using Jackie.Utility;

#endregion

namespace Jackie.Update.Entities
{
    /// <summary> 
    /// 更新配置项
    /// </summary>
    [DataContract(Name = "updateFile", Namespace = "http://serializer.skomart.net/updateFile")]
    [KnownType(typeof(OperateType))]
    public class UpdateFileInfo
    {
        /// <summary>
        /// 路径
        /// </summary>
        [DataMember]
        public string Path { get; set; }
        /// <summary>
        /// 操作类型
        /// </summary>
        [DataMember]
        public OperateType OperateType { get; set; }
        /// <summary>
        /// 文件大小
        /// </summary>
        [DataMember]
        public long Length { get; set; }
        /// <summary>
        /// 文件哈希值
        /// </summary>
        [DataMember]
        public string HashCode { get; set; }

        public UpdateFileInfo()
        {
        }

        public UpdateFileInfo(XmlNode fileNode)
        {
            Path = fileNode.Attributes["path"].Value;
            OperateType = (OperateType)Enum.Parse(typeof(OperateType),
                           fileNode.Attributes["operateType"].Value);
            Length = JackieConvertor.ToLongThrowException(fileNode.Attributes["fileLength"].Value);
            HashCode = fileNode.Attributes["hashCode"].Value;
        }

        public XmlElement CreateConfigNode(XmlDocument doc)
        {
            var fileElement = doc.CreateElement("File");

            var pathAttr = doc.CreateAttribute("path");
            pathAttr.Value = Path;
            fileElement.Attributes.Append(pathAttr);

            var operateAttr = doc.CreateAttribute("operateType");
            operateAttr.Value = OperateType.ToString();
            fileElement.Attributes.Append(operateAttr);

            var lenghtAttr = doc.CreateAttribute("fileLength");
            lenghtAttr.Value = Length.ToString();
            fileElement.Attributes.Append(lenghtAttr);

            var hashCodeAttr = doc.CreateAttribute("hashCode");
            hashCodeAttr.Value = HashCode;
            fileElement.Attributes.Append(hashCodeAttr);

            return fileElement;
        }
    }
}